/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf;

import com.controllers.CreditController;
import com.controllers.CrudUtil;
import com.entities.Credit;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletContext;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author ben
 */
@Named(value = "creditView")
@SessionScoped
public class CreditView implements Serializable {

    /**
     * Creates a new instance of CreditView
     */
    @EJB
    private CrudUtil crudUtil;
    @EJB
    private CreditController creditController;
    @ManagedProperty(value = "creditAction")
    CreditAction creditAction;
    @ManagedProperty(value = "companyDenominationView")
    CompanyDenominationView companyDenominationView;
    private Long selectedCreditId;
    private List<Credit> credits;
    private Credit currentCredit;
    private Credit editedCredit;
    private int activeIndex = 0;

    public Long getSelectedCreditId() {
        return selectedCreditId;
    }

    public String editCredit(Long id) {
        try {
            editedCredit = creditController.findById(id);
            if (editedCredit != null) {
                editedCredit.getId();
                return "c_edit.xhtml?faces-redirect=true";
            } else {
                creditTab(3);
                System.out.println("Unable to find matching id");
                return "credit.xhtml?faces-redirect=true";
            }
        } catch (Exception e) {
            System.out.println("Exception here " + e.getMessage());
            return null;
        }
    }

    public void creditTab( int i) {
        setActiveIndex(i);
    }

    public void companyTab(int i) {
        setActiveIndex(i);
    }

    public void currecyTab(int i) {
        setActiveIndex(i);
    }

    public void vouchertab(int i) {
        setActiveIndex(i);
    }

    public String updateEditedcredit() {
        try {
            editedCredit.getId();
            creditAction.setCompanyDenominationID(editedCredit.getId());
            if (crudUtil.update(editedCredit)) {
               // creditTab(3);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successfully Updated"));
                return "index.xhtml?faces-redirect=true";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unable to update Updated"));
                return "c_edit.xhtml?faces-redirect=true";
            }
        } catch (Exception e) {
            System.out.println("Exception cuaght here: " + e.getMessage());
            return null;
        }
    }

    public void deleteCredit(Long id) {
        try {
            editedCredit = creditController.findById(id);
            if (editedCredit != null) {
                System.out.println("Id found which is : " + " " + id);
                if (crudUtil.delete(editedCredit)) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successfully Deeted"));
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Unable to delete Credit"));
                }
            } else {
                System.out.println("Such credit with this id does not exist");
            }

        } catch (Exception e) {
            System.out.println("Exception Caught here " + e.getMessage());
        }
    }

    public void setSelectedCreditId(Long selectedCreditId) {
        System.out.println("Set Credit has been called");
        //Look for the Credit 
        currentCredit = creditController.findById(selectedCreditId);


        if (currentCredit == null) {
            //throw an error if we cannot find the entity
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unable to create Credit", "Unable to create Credit"));

        } else {

            this.selectedCreditId = selectedCreditId;

            //try to save the image of the current credit to disk
            try {
                System.out.println(currentCredit);
                ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                String newFileName = servletContext.getRealPath("") + File.separator + "resources"
                        + File.separator + File.separator + "images" + File.separator + currentCredit.getId() + ".png";

                FileImageOutputStream imageOutput;

                //Actual saving lines
                imageOutput = new FileImageOutputStream(new File(newFileName));
                imageOutput.write(currentCredit.getImage(), 0, currentCredit.getImage().length);
                imageOutput.close();


            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }

    public CreditView() {
        currentCredit = new Credit();
    }

    public Credit getCurrentCredit() {
        return currentCredit;
    }

    public void setCurrentCredit(Credit currentCredit) {
        this.currentCredit = currentCredit;
    }

    public void setCredits(List<Credit> credits) {
        this.credits = credits;
    }

    public List<Credit> getAllCredits() {
        return creditController.getAllCredit();
    }
    private LazyDataModel model;

    public CreditController getCreditController() {
        return creditController;
    }

    public void setCreditController(CreditController creditController) {
        this.creditController = creditController;
    }

    public LazyDataModel getModel() {
        return model;
    }

    public void setModel(LazyDataModel model) {
        this.model = model;
    }

    public Credit getEditedCredit() {
        return editedCredit;
    }

    public void setEditedCredit(Credit editedCredit) {
        this.editedCredit = editedCredit;
    }

    public CrudUtil getCrudUtil() {
        return crudUtil;
    }

    public void setCrudUtil(CrudUtil crudUtil) {
        this.crudUtil = crudUtil;
    }

    public int getActiveIndex() {
        return activeIndex;
    }

    public void setActiveIndex(int activeIndex) {
        this.activeIndex = activeIndex;
    }

    public CreditAction getCreditAction() {
        return creditAction;
    }

    public void setCreditAction(CreditAction creditAction) {
        this.creditAction = creditAction;
    }

    public CompanyDenominationView getCompanyDenominationView() {
        return companyDenominationView;
    }

    public void setCompanyDenominationView(CompanyDenominationView companyDenominationView) {
        this.companyDenominationView = companyDenominationView;
    }
}
